package tech.aistar.day08.equal;

/**
 * 本类用来演示:
 *
 * @author: success
 * @date: 2021/3/17 8:35 上午
 */
public class Book implements Cloneable{
    private int id;

    private String isbn;

    private String bookName;

    private double price;

    //一个图书对应一个图书信息
    private BookInfo bookInfo;

    public Book() {
    }

    public Book(int id, String isbn, String bookName, double price) {
        this.id = id;
        this.isbn = isbn;
        this.bookName = bookName;
        this.price = price;
    }

    public void setBookInfo(BookInfo bookInfo) {
        this.bookInfo = bookInfo;
    }

    public BookInfo getBookInfo() {
        return bookInfo;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {

        this.id = id;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public boolean equals(Object o) {
        System.out.println("equals...");
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;

        if (id != book.id) return false;
        return isbn != null ? isbn.equals(book.isbn) : book.isbn == null;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (isbn != null ? isbn.hashCode() : 0);
        System.out.println("result:"+result);
        return result;
    }

    //    //b5.equals(b6)
//    //b5 -> this
//    //b6 -> obj
//    @Override
//    public boolean equals(Object obj){
//        //1. 非空性
//        if(obj == null)
//            return false;
//        //2. 自反性
//        if(this == obj)
//            return true;
//        //3. 一致性
//        //进行类型判断
//        if(obj instanceof Book){
//            //进行强制类型转换
//            Book b = (Book) obj;
//            //确定对象中的哪些属性是一样的时候,那么才认为是同一个对象
//            //① - 只要俩个对象的id[对象的标志]是一样的,就返回true
//            //return this.id == b.id;
//
//            //② - id和编号一致的时候就返回true
//            //return this.id == b.id && this.isbn.equals(b.isbn);
//
//            //③ - id和编号以及关联的Bookinfo对象的id也要一样,才返回true
//            return this.id == b.id && this.isbn.equals(b.isbn) && this.bookInfo.equals(b.bookInfo);
//        }
//        return false;
//    }


    //浅拷贝
//    @Override
//    public Object clone() throws CloneNotSupportedException {
//        return super.clone();
//    }

    //深拷贝
    @Override
    public Object clone() throws CloneNotSupportedException {
        //1. 新建一个Book对象
        Book b = new Book();

        b.setId(this.id);
        b.setBookName(this.bookName);
        b.setIsbn(this.isbn);
        b.setPrice(this.price);

        BookInfo info = this.bookInfo;
        //因为外部的b5对象有可能没有设置bookInfo
        if(null!=info){
            BookInfo infoCopy = new BookInfo();
            infoCopy.setId(info.getId());
            infoCopy.setRemark(info.getRemark());
            b.setBookInfo(infoCopy);
        }
        return b;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", isbn='" + isbn + '\'' +
                ", bookName='" + bookName + '\'' +
                ", price=" + price +
                '}';
    }
}
